//
//  HomeViewModel.swift
//  WBDemo
//
//  Created by Jin on 09/05/2018.
//  Copyright © 2018 com.baidu.123. All rights reserved.
//

import Foundation
import UIKit

struct HomeViewModel:Codable {
    
    var text : String?
    var source : String?
    var created_at : Date
    var mid : String
    var id : Int64
    var retweeted_status : HomePage?
    
    //user
    var screen_name : String? //用户昵称
    var user_id : Int64
    var profile_image_url : String? //用户头像
    //用户认证类型 0 个人认证 vip| 2 3 5 企业认证 enterprise_vip| 220 微博达人 grassroot
    var verified_type : Int  = -1
    var mbrank : Int = 0 //用户会员等级
    var pic_urls : [[String:String]]? //微博配图
    
    enum HomePageCodingKeys:String,CodingKey {
        case text
        case source
        case created_at
        case mid
        case id
        case user
        case pic_urls
        case retweeted_status
    }
    
    enum UserInfoCodingKeys : String,CodingKey {
        case screen_name
        case user_id = "id"
        case profile_image_url
        case verified_type
        case mbrank
    }

    init(text:String?,source:String?,created_at:Date,mid:String,id:Int64,user_id:Int64,screen_name:String?,profile_image_url:String?,verified_type:Int,mbrank:Int,pic_urls:[[String:String]]?,retweeted_status:HomePage?) {
        self.text = text
        self.source = source
        self.created_at = created_at
        self.mid = mid
        self.id = id
        self.user_id = user_id
        self.screen_name = screen_name
        self.profile_image_url = profile_image_url
        self.verified_type = verified_type
        self.mbrank = mbrank
        self.pic_urls = pic_urls
        self.retweeted_status = retweeted_status
    }
    
    init(from decoder:Decoder) throws {
        //微博相关
        let container = try decoder.container(keyedBy: HomePageCodingKeys.self)
        let text = try container.decodeIfPresent(String.self, forKey: .text)
        let source = try container.decodeIfPresent(String.self, forKey: .source)
        let created_at = try container.decode(Date.self, forKey: .created_at)
        let mid = try container.decode(String.self, forKey: .mid)
        let id = try container.decode(Int64.self, forKey: .id)
        let pic_urls = try container.decodeIfPresent([[String:String]].self, forKey: .pic_urls)
        //微博用户信息
        let user = try container.nestedContainer(keyedBy: UserInfoCodingKeys.self, forKey: .user)
        let user_id = try user.decode(Int64.self, forKey: .user_id)
        let screen_name = try user.decodeIfPresent(String.self, forKey: .screen_name)
        let profile_image_url = try user.decodeIfPresent(String.self, forKey: .profile_image_url)
        let verified_type = try user.decode(Int.self, forKey: .verified_type)
        let mbrank = try user.decode(Int.self, forKey: .mbrank)
        //转发微博信息
        let retweeted_status = try container.decodeIfPresent(HomePage.self, forKey: .retweeted_status)
        
        self.init(text: text, source: source, created_at: created_at, mid: mid, id: id,user_id:user_id,screen_name:screen_name,profile_image_url:profile_image_url,verified_type:verified_type,mbrank:mbrank,pic_urls:pic_urls,retweeted_status:retweeted_status)
    }
}


